/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef GAMESETTINGS_H
#define GAMESETTINGS_H

#include "defines.h"     // include this before anything else.
#include <QObject>
#include <QDate>

// Defines
#define GS_DIFFICULTY "difficulty"
#define GS_DIFFICULTY_DEFAULT 2
#define GS_SENSITIVITY "sensitivity"
#define GS_SENSITIVITY_DEFAULT 5
#define GS_MUSIC_LEVEL "music_level"
#define GS_MUSIC_LEVEL_DEFAULT 6
#define GS_SOUND_LEVEL "sound_level"
#define GS_SOUND_LEVEL_DEFAULT 6
#define GS_LEFT_HANDED "left_handed"
#define GS_LEFT_HANDED_DEFAULT false
#define GS_REWIND "rewind"
#define GS_REWIND_DEFAULT false

#ifdef SMASHMINER_CONFIG_ENABLE_ROTATION_SENSOR
#define GS_GROUP_ROTATION "RotationSettings"
#define GS_ROTATION_SENSITIVITY_X "rotation_sensitivity_x"
#define GS_ROTATION_SENSITIVITY_X_DEFAULT 4.0
#define GS_ROTATION_SENSITIVITY_Y "rotation_sensitivity_y"
#define GS_ROTATION_SENSITIVITY_Y_DEFAULT 4.0
#endif

#ifdef SMASHMINER_CONFIG_ENABLE_ACCELERATION_SENSOR
#define GS_GROUP_ACCELERATION "AccelerationSettings"
#define GS_ACCELERATION_SENSITIVITY_Z "acceleration_sensitivity_z"
#define GS_ACCELERATION_SENSITIVITY_Z_DEFAULT 5.0
#endif

#define GS_GROUP_HIGHSCORES "HighScores"


// Forward declarations
class QSettings;
class HighScoreModel;

class GameSettings : public QObject
{

Q_OBJECT

// Properties exposed to QML
Q_PROPERTY(int difficultyLevel READ difficultyLevel WRITE setDifficultyLevel NOTIFY difficultyChanged)
Q_PROPERTY(int steeringSensitivity READ steeringSensitivity WRITE setSteeringSensitivity NOTIFY sensitivityChanged)
Q_PROPERTY(int musicLevel READ musicLevel WRITE setMusicLevel NOTIFY musicLevelChanged)
Q_PROPERTY(int soundLevel READ soundLevel WRITE setSoundLevel NOTIFY soundLevelChanged)
Q_PROPERTY(bool leftHanded READ leftHanded WRITE setLeftHanded NOTIFY leftHandedChanged)
Q_PROPERTY(bool endRewind READ endRewind WRITE setEndRewind NOTIFY endRewindChanged)

public:
    struct HighScore {
        QString name;
        int score;
        QString time;
        QDate date;
    };

    GameSettings(QObject *parent);

    inline int difficultyLevel() { return difficulty; }
    inline int steeringSensitivity() { return sensitivity; }
    inline int musicLevel() { return musiclevel; }
    inline int soundLevel() { return soundlevel; }
    inline bool leftHanded() { return lefthanded; }
    inline bool endRewind() { return rewind; }
    void readHighScore();

    Q_INVOKABLE int setHighScore(QString, int, QString);
    Q_INVOKABLE bool isHighScore(int);
    Q_INVOKABLE HighScoreModel* getHighScoreModel();

#ifdef SMASHMINER_CONFIG_ENABLE_ROTATION_SENSOR
    inline FPtype rotationSensitivityX() { return rotationsensitivity_x; }
    inline FPtype rotationSensitivityY() { return rotationsensitivity_y; }
#endif  // SMASHMINER_CONFIG_ENABLE_ROTATION_SENSOR

#ifdef SMASHMINER_CONFIG_ENABLE_ACCELERATION_SENSOR
    inline int accelerationSensitivityZ() { return accelerationsensitivity_z; }
#endif  // SMASHMINER_CONFIG_ENABLE_ROTATION_SENSOR

public slots:
    Q_INVOKABLE void save();
    Q_INVOKABLE void resetToDefaults();
    void setDifficultyLevel(int);
    void setSteeringSensitivity(int);
    void setMusicLevel(int);
    void setSoundLevel(int);
    void setLeftHanded(bool);
    void setEndRewind(bool);
#ifdef SMASHMINER_CONFIG_ENABLE_ROTATION_SENSOR
    void setRotationSensitivityX(FPtype);
    void setRotationSensitivityY(FPtype);
#endif  // SMASHMINER_CONFIG_ENABLE_ROTATION_SENSOR

#ifdef SMASHMINER_CONFIG_ENABLE_ACCELERATION_SENSOR
    void setAccelerationSensitivityZ(int);
#endif  // SMASHMINER_CONFIG_ENABLE_ACCELERATION_SENSOR

signals:
    void difficultyChanged();
    void sensitivityChanged();
    void musicLevelChanged(int);
    void soundLevelChanged(int);
    void leftHandedChanged(bool);
    void endRewindChanged(bool);

private:
    /// Game settings
    QSettings *settings;

#ifdef SMASHMINER_CONFIG_ENABLE_ROTATION_SENSOR
    FPtype rotationsensitivity_x;
    FPtype rotationsensitivity_y;
#endif  // SMASHMINER_CONFIG_ENABLE_ROTATION_SENSOR

#ifdef SMASHMINER_CONFIG_ENABLE_ACCELERATION_SENSOR
    int accelerationsensitivity_z;
#endif

    /// Game difficulty level from 1-3, default = 2
    int difficulty;

    /// Game control (tilt) sensitivity from 1-10, default = 5
    int sensitivity;

    /// Game music level from 0-10, default 6
    int musiclevel;

    /// Game sound effect level from 0-10, default 6
    int soundlevel;

    /// Left handed mode, default false
    bool lefthanded;

    /// Rewind mode, default false
    bool rewind;

    /// Current high scores list
    QList<HighScore> highScoreList;

    /// Current high scores model
    HighScoreModel *highScoreModel;

};

#endif // GAMESETTINGS_H
